package com.hyh.eblog.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyh.eblog.entity.Category;
import com.hyh.eblog.service.CategoryService;
import com.hyh.eblog.service.PostService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.List;

/**
 * @author Summerday
 */
@Component
public class ContextStartUp implements ApplicationRunner, ServletContextAware{

    ServletContext servletContext;

    @Resource
    private CategoryService categoryService;

    @Resource
    private PostService postService;

    //启动时调用
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Category> categories = categoryService.list(new QueryWrapper<Category>()
                .eq("status", 0));
        servletContext.setAttribute("categories", categories);
        //初始化本周热议
        postService.initWeekRank();
    }

    //应用级别注入
    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
